package changgou;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @ClassName EurekaApplication
 * @Description 注册中心
 * @Author 传智播客
 * @Date 11:31 2019/11/10
 * @Version 2.1
 **/
@SpringBootApplication
@EnableEurekaClient
public class GatewayWebApplication8001 {

    public static void main(String[] args) {
        SpringApplication.run(GatewayWebApplication8001.class, args);
    }

    /**
     * 创建用户唯一标识使用IP作为用户唯一标识， 来根据IP进行限流操作
     */
    @Bean(name = "ipKeyResolver")
    public KeyResolver userKeyResolver() {
        return new KeyResolver() {
            @Override
            public Mono<String> resolve(ServerWebExchange exchange) {
                String ip = exchange.getRequest().getRemoteAddress().getHostString();
//                return  Mono.just("创建用户唯一标识使用IP作为用户唯一标识， 来根据IP进行限流操作");
                return Mono.just(ip);
            }
        };
    }
}
